package com.esd.db.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.esd.db.dao.VtdParameterMapper;
import com.esd.db.dao.userMapper;
import com.esd.db.model.VtdParameter;
import com.esd.db.service.VtdParameterService;
@Service("VtdParameterService")
public class VtdParameterServiceImpl implements VtdParameterService {
	@Autowired
	VtdParameterMapper paramMapper;
	@Autowired
	userMapper userMapper;
	
	@Override
	public List<VtdParameter> selectActiveAll() {
		return paramMapper.selectActiveALL();
	}
	
	@Override
	public List<VtdParameter> selectParamByType(String type) {
		return paramMapper.selectParamByType(type);
	}
	
	@Override
	public int countAllByParamType(String type) {
		return paramMapper.countAllByParamType(type);
	}

	@Override
	public int saveParameter(VtdParameter param) {
		return paramMapper.saveParameter(param);
	}

	@Override
	public boolean isParamExist(String paramName, String paramType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramName", paramName);
		map.put("paramType", paramType);
		int num = paramMapper.countParamByParamNameAndType(map);
		if(num>=1){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public List<VtdParameter> selectAllParamByType(String type) {
		return paramMapper.selectAllParamByType(type);
	}

	@Override
	public int changeParamActiveById(int paramId) {
		VtdParameter param = paramMapper.selectParamById(paramId);
		boolean newActive;
		if(param.getIsActive()){
			newActive = false;
		}else{
			newActive = true;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("newActive", newActive);
		return paramMapper.updateParamActiveById(map);
	}

	@Override
	public int updateParamNameById(int paramId, String paramName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("paramName", paramName);
		return paramMapper.updateParamNameById(map);
	}

	@Override
	public int updateParamValueById(int paramId, String paramValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("paramValue", paramValue);
		return paramMapper.updateParamValueById(map);
	}

	@Override
	public int updateRemarksById(int paramId, String remarks) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("remarks", remarks);
		return paramMapper.updateRemarksById(map);
	}

	@Override
	public String selectMaxParamValueByParamType(String paramType) {
		return paramMapper.selectMaxParamValueByParamType(paramType);
	}

	@Override
	public String selectRemarksByParamVlaueAndParamType(String paramVlaue, String paramType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramVlaue", paramVlaue);
		map.put("paramType", paramType);
		return paramMapper.selectRemarksByParamVlaueAndParamType(map);
	}

	@Override
	public VtdParameter selectParamById(int id) {
		return paramMapper.selectParamById(id);
	}

	@Override
	public int updateBackup1ByParamValueAndParamType(String paramVlaue, String paramType, String backup1) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramVlaue", paramVlaue);
		map.put("paramType", paramType);
		map.put("backup1", backup1);
		return paramMapper.updateBackup1ByParamValueAndParamType(map);
	}

	@Override
	public VtdParameter getParamValueByParamTypeAndParamName(String paramType, String paramName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramName", paramName);
		map.put("paramType", paramType);
		return paramMapper.selectParamValueByParamTypeAndParamName(map);
	}

	@Override
	public int updateBackup2ById(int paramId, String subsidyFinal) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("subsidyFinal", subsidyFinal);
		return paramMapper.updateBackup2ByParamId(map);
	}

	@Override
	public double getBackup2ByParamValueAndParamType(String workGroupCode, String workGroup) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("value", workGroupCode);
		map.put("type", workGroup);
		String backup2 = paramMapper.selectBackup2ByParamValueAndParamType(map);
		return Double.parseDouble(backup2);
	}

	@Override
	public boolean changeParamActiveAndChangeUserStatusByParamId(int paramId) {
		//查询参数信息
		VtdParameter param = paramMapper.selectParamById(paramId);
		//修改可用状态
		boolean newActive;
		if(param.getIsActive()){
			newActive = false;
		}else{
			newActive = true;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramId", paramId);
		map.put("newActive", newActive);
		int count = paramMapper.updateParamActiveById(map);
		// 修改用户状态
		if(count==1){
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("workGroupCode", param.getParamValue());
			if(newActive){
				map2.put("userStatus", true);
			}else{
				map2.put("userStatus", false);
			}
			int count2 = userMapper.changeUserStatus(map2);
			if(count2>0){
				return true;
			}else{
				return false;
			}
		}else{
			
			return false;
		}
	}
	
}
